package com.qingpeng.pay.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserInfoActivity extends BaseActivity {

    @BindView(R.id.title)
    TitleBar title;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_wechat)
    EditText edWechat;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_crak)
    EditText edCrak;
    @BindView(R.id.ed_crak_message)
    EditText edCrakMessage;
    @BindView(R.id.pay)
    TextView pay;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void onResume() {
        super.onResume();
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        edPhone.setText(APP.userInfo.getAccount());
        edPhone.setKeyListener(null);
        if (StringUtils.isEmpty(APP.userInfo.getWx_name())){
            return;
        }
        edWechat.setText(APP.userInfo.getWx_name());
        edName.setText(APP.userInfo.getName());
        edCrak.setText(APP.userInfo.getDeposit_card());
        edCrakMessage.setText(APP.userInfo.getDeposit_name());

        edPhone.setKeyListener(null);
        edWechat.setKeyListener(null);
        edName.setKeyListener(null);
        edCrak.setKeyListener(null);
        edCrakMessage.setKeyListener(null);
        pay.setText("已提交");
        pay.setClickable(false);
    }

    @OnClick(R.id.pay)
    public void onViewClicked() {
        HashMap<String, String> map = new HashMap<>();
        if (!StringUtils.isEmpty(edPhone.getText().toString())
                &&!StringUtils.isEmpty(edWechat.getText().toString())
                &&!StringUtils.isEmpty(edName.getText().toString())
                &&!StringUtils.isEmpty(edCrak.getText().toString())
                &&!StringUtils.isEmpty(edCrakMessage.getText().toString())){
            map.put("mobile",edPhone.getText().toString());
            map.put("wx_name",edWechat.getText().toString());
            map.put("name",edName.getText().toString());
            map.put("deposit_card",edCrak.getText().toString());
            map.put("deposit_name",edCrakMessage.getText().toString());
            Call<NullBean> realname = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).realname(map);
            realname.enqueue(new Callback<NullBean>() {
                @Override
                public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                    NullBean body = response.body();
                    if (body.getStatus()!=1){
                        ToastUtils.showToast(UserInfoActivity.this,body.getInfo());
                        return;
                    }
                    ToastUtils.showToast(UserInfoActivity.this,"绑定成功");
                    finish();
                }
                @Override
                public void onFailure(Call<NullBean> call, Throwable t) {
                    ToastUtils.showToast(UserInfoActivity.this, "网络请求错误,请稍后重试");
                }
            });
        }else{
            ToastUtils.showToast(UserInfoActivity.this,"请完整填写信息");
        }
    }
}
